import random
import string
def generate_password(length=12):
    "“”生成包含大小写字母和数字的随机密码"""
    characters = string.ascii_letters + string.digits
    password = ''.join(random.choice(characters) for _ in range(length))
    return password


def generate_strong_password(length=12):
    """生成必须包含大小写字母、数字和特殊字符的密码"""

    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    digits = string.digits
    special = '!@#$%^&*'


    password = [
        random.choice(lower),
        random.choice(upper),
        random.choice(digits),
        random.choice(special)
    ]
    all_chars = lower + upper + digits + special
    password.extend(random.choice(all_chars) for _ in range(length - 4))

    random.shuffle(password)
    return ''.join(password)

print(generate_strong_password())




import secrets
import string

def generate_secure_password(length=16):
    """生成加密安全的随机密码"""
    alphabet = string.ascii_letters + string.digits + '!@#$%^&*'
    while True:
        password = ''.join(secrets.choice(alphabet) for _ in range(length))
        if (any(c.islower() for c in password)
            and any(c.isupper()for c in password)
            and any(c in '!@#$%^&*' for c in password)):
            return password
print(generate_secure_password())


